$(document).ready(function(){

    var $corporation = $('#corporation');
    var $company = $('#company');
    var $department= $('#department');

    var $hotelId= $('#hotel_id');
    var $shopId= $('#shop_id');

    var $customerCode = $('#customer_code');
    var $reCustCode = $('#re_cust_code');

    if(!EOVA_FORM)
        return;

    var cmbCorporation = $corporation.eovacombo();
    var cmbCompany = $company.eovacombo();
    var cmbDepartment = $department.eovacombo();
    $hotelId.eovafind().readonly(true);
    $shopId.eovafind().readonly(true);
    $customerCode.eovatext().readonly(true);
    $reCustCode.eovatext().readonly(true);

    // 初始禁用
    cmbCompany.readonly(true);
    cmbDepartment.readonly(true);

    if($corporation !=""){
        cmbCompany.readonly(false);
        $company.eovacombo({exp : 'selectOrgByLv2AndPid,' + 20000000}).reload();
    }

    // 省级联市
    $corporation.eovacombo({onChange: function (oldValue, newValue) {
            cmbCompany.setValue("");
            cmbDepartment.setValue("");
            if (newValue == "") {
                cmbCompany.readonly(true)
                return;
            }
            cmbCompany.readonly(false);
            $company.eovacombo({exp : 'selectOrgByLv2AndPid,' + newValue}).reload();
        }});

    // 省级联市
    $company.eovacombo({onChange: function (oldValue, newValue) {
            cmbDepartment.setValue("");
            if (newValue == "") {
                cmbDepartment.readonly(true);
                return;
            }
            cmbDepartment.readonly(false);
            $department.eovacombo({exp : 'selectOrgByLv3AndPid,' + newValue}).reload();
        }});

    $department.eovacombo({onChange: function (oldValue, newValue){
            $hotelId.eovafind().setValue("");
            if("" == newValue){
                $hotelId.eovafind().readonly(true);
                return;
            }
            $hotelId.eovafind().readonly(false);
            $hotelId.eovafind({exp:'selectHotelByDepartment,'+newValue});

        }});

    $hotelId.eovafind({onChange:function(oldValue, newValue){
            $shopId.eovafind().setValue("");
            if("" == newValue){
                $shopId.eovafind().readonly(true);
                return;
            }
            $shopId.eovafind().readonly(false);
            $shopId.eovafind({exp:'selectShopByHotel,'+newValue});
        }});

    $shopId.eovafind({onChange:function(oldValue, newValue){
            $customerCode.eovatext().setValue("");
            $reCustCode.eovatext().setValue("");
            if("" == newValue){
                $customerCode.eovatext().readonly(true);
                $reCustCode.eovatext().readonly(true);
                return;
            }
            $customerCode.eovatext().readonly(false);
            $reCustCode.eovatext().readonly(false);

            $.post('/shop/getcustcodeById/'+newValue,function(res){
                if(res.success){
                    $customerCode.eovatext().setValue(res.data.customer_code);
                    $reCustCode.eovatext().setValue(res.data.re_cust_code);
                }
            });

        }});

});